// pay.js
var api = require('../../../../config/api.js');
var util = require('../../../../utils/util.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    moneyValue: '',
    cpPrices:'',
    RedModal:true

  },
  //获取输入金额
  moneyValue: function (e) {
    var money = e.detail.value
    if (!/^\d+(\.\d{0,2})?$/.test(money)){
      wx.showModal({
        title: '提示',
        content: '金额应该保留两位小数'
      })
    }else{
      this.setData({
        moneyValue: money
      })
    }
   
  },
  //展示支付页面信息
  payContentShow:function(){
    let that=this
    util.request(api.ShopPayUrl, { sp_id: that.data.sp_id}).then(function(res){
      
      if(res.data.code == 200){
        that.setData({
          payContentInfos: res.data.data
        })
      }
    })

  },
  //去确认支付
  payNow:function(){
    let that = this
    var moneyValue = that.data.moneyValue
    console.log(app.globalData.unionid)
    util.request(api.wxPayUrl,{
      money: moneyValue,
      sp_id: that.data.sp_id,
      unionid: app.globalData.unionid

    },'POST').then(function(res){

      if (res.data.code === 5000) {
        var benefitMoney = res.data.data.money
        var benefitId = res.data.data.id
        console.log(benefitMoney, benefitId)
        wx.showModal({
          title: '红包支付',
          content: '应付' + benefitMoney + '元' + '\n' + '实付0元' + '\n' + '红包抵扣' + benefitMoney + '元',
          success: function (res) {
            if (res.confirm) {
              util.request(api.PursePayUrl, { 
                id: benefitId, unionid: app.globalData.unionid
              },'POST').then(function(res){
                 console.log(res)
                 var benefitOrder = res.data.data.order
                 that.playRed(benefitOrder)
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (res.data.code === 200) {
        var appId = res.data.wxpay.appId
        var nonceStr = res.data.wxpay.nonceStr
        var packages = res.data.wxpay.package
        var paySign = res.data.wxpay.paySign
        var signType = res.data.wxpay.signType
        var timeStamp = res.data.wxpay.timeStamp
        var payOrder = res.data.order     //wx2018011001385862738111
        wx.requestPayment({
          'timeStamp': timeStamp,
          'nonceStr': nonceStr,
          'package': packages,
          'signType': signType,
          'paySign': paySign,
          success: function () {
            wx.showToast({
              title: '支付成功',
              success: function () {
                that.playRed(payOrder)
              }
            });
          },
          fail: function () {
            wx.showToast({
              title: '支付失败',
            });
          }
        })
      } else {
        wx.showToast({
          title: '支付出错,请重新登录'
        })
      }
    })
    
  },
  //弹出红包
  playRed: function (order){
    let that = this
    util.request(api.RedPurse, { order: order, unionid: app.globalData.unionid },'POST').then(function(res){
      console.log(res)
      if (res.data.code == 200) {
        that.setData({
          cpPrices: res.data.data,
          RedModal: !that.data.RedModal
        })
      } else if (res.data.code == 4009) {
        that.hideRedModal()
      } else {
        wx.showToast({
          title: 'unionid无效,请重新登录',
        })
      }
    })

  },
  //隐藏红包
  hideRedModal:function(){
    this.setData({
      RedModal: true
    });
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.setData({
       sp_id: parseInt(options.sp_id)
     })
     this.payContentShow()
  },


})